// 对ajax封装
import axios from 'axios'
import store from '@/store'
// 引入依赖面
import 'nprogress/nprogress.css'
import NProgress from "nprogress";

import { getUserTempId } from "@/utils/getUserTempId"

// 创建实例
const ajax = axios.create({
    baseURL: '/api',
    timeout: 1000,
})

// 请求拦截器
ajax.interceptors.request.use(config => {
    NProgress.start()
    const token = store.state.passport.token;
    config.headers.token = token;

    config.headers.userTempId = getUserTempId()
    return config
})

// 响应拦截器
ajax.interceptors.response.use(
    response => {
        NProgress.done()
        return response.data
    },
    err => {
        NProgress.done()
        return Promise.reject(new Error(err.message))
    }
)

export default ajax
